package com.lanou.myapplication.movieAdapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.lanou.myapplication.R;
import com.lanou.myapplication.entity.Entertainment;
import com.lanou.myapplication.entity.Movie;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/27.
 */
public class MovierecyclerAdapter extends
        RecyclerView.Adapter<MovierecyclerAdapter.MovieViewHolder> {
   private List<Movie.T1348648650048Entity> t1348648650048Entities;//存放类型
    private RequestQueue queue;//定义一个volley请求队列

    public MovierecyclerAdapter(Context c){
        queue = Volley.newRequestQueue(c);
    }

    public void  addData(List<Movie.T1348648650048Entity> datas){
        t1348648650048Entities =datas;
        notifyDataSetChanged();
    }
   class MovieViewHolder extends  RecyclerView.ViewHolder{
       private ImageView left_image;
       private TextView item_title;
       private TextView item_content;
       private TextView item_abstract;
       private ImageView item_image_0;
       private ImageView item_image_1;
       private ImageView item_image_2;

       public MovieViewHolder(View v){
           super(v);
           left_image=(ImageView)v.findViewById(R.id.left_image3);
           item_title=(TextView)v.findViewById(R.id.item_title3);
           item_content=(TextView)v.findViewById(R.id.item_content3);
//           item_abstract=(TextView)v.findViewById(R.id.item_abstract2);
//           item_image_0=(ImageView)v.findViewById(R.id.item_image_02);
//           item_image_1=(ImageView)v.findViewById(R.id.item_image_12);
//           item_image_2=(ImageView)v.findViewById(R.id.item_image_22);
       }
   }
    @Override
    public int getItemCount() {
        return t1348648650048Entities != null && t1348648650048Entities.size() > 0
                ? t1348648650048Entities.size() : 0;
    }
    @Override
    public void onBindViewHolder(
            MovieViewHolder holder, int position) {
        if(t1348648650048Entities != null && t1348648650048Entities.size() > 0){
            Movie.T1348648650048Entity t = t1348648650048Entities.get(position);
            //设置文字
            holder.item_title.setText(t.getTitle());
            holder.item_content.setText(t.getDigest());
//            holder.item_abstract.setText(t.getTitle());

            //在进行图片请求前先判断返回的集合中是否有图片
            if(t.getImgsrc() != null
                    && t.getImgsrc().length() > 0){
                //取集合中的第0张图片地址
                String imgUrl = t.getImgsrc();
                //网络拉取图片并显示
                getBitmap(imgUrl, holder.left_image);
            }
//            if(t.getImgextra() != null
//                    && t.getImgextra().size() > 0){
//                //取集合中的第0张图片地址
//                String imgUrl = t.getImgextra().get(g);
//                //网络拉取图片并显示
//                getBitmap(imgUrl, holder.imgIv);
//            }
//            if(t.getImages() != null
//                    && t.getImages().size() > 0){
//                //取集合中的第0张图片地址
//                String imgUrl = t.getImages().get(0);
//                //网络拉取图片并显示
//                getBitmap(imgUrl, holder.imgIv);
//            }
//            if(t.getImages() != null
//                    && t.getImages().size() > 0){
//                //取集合中的第0张图片地址
//                String imgUrl = t.getImages().get(0);
//                //网络拉取图片并显示
//                getBitmap(imgUrl, holder.imgIv);
//            }
        }
    }
    /**
     * 通过Volley请求图片
     * @param url 图片地址
     * @param imageView 要显示的图片组件
     * **/
    public void getBitmap(String url, final ImageView imageView) {

        ImageRequest request = new ImageRequest(url,
                new Response.Listener<Bitmap>() {

                    @Override
                    public void onResponse(Bitmap response) {
                        //将返回的bitmap图片对象设置到图片组件中
                        imageView.setImageBitmap(response);
                    }
                },
                300, 300, Bitmap.Config.ARGB_8888,
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(
                            VolleyError error) {
                        error.printStackTrace();
                    }
                }){
            @Override
            public Map<String, String> getHeaders()
                    throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };

        queue.add(request);//将本次图片请求加入到请求队列中
    }



    @Override
    public MovieViewHolder onCreateViewHolder(
            ViewGroup viewGroup, int position) {
        //获得布局加载器
        LayoutInflater inflater = LayoutInflater.from(
                viewGroup.getContext());

        //加载行布局文件
        View v = inflater.inflate(
                R.layout.items_newsthree, null);
        return new MovieViewHolder(v);//构建组件缓存对象并返回
    }
}
